package com.cxs.xiaomi.service.impl;

import com.cxs.xiaomi.dao.AddressDao;
import com.cxs.xiaomi.dao.CartDao;
import com.cxs.xiaomi.dao.OrderDao;
import com.cxs.xiaomi.dao.OrderDetailDao;
import com.cxs.xiaomi.dao.impl.AddressDaoImpl;
import com.cxs.xiaomi.dao.impl.CartDaoImpl;
import com.cxs.xiaomi.dao.impl.OrderDaoImpl;
import com.cxs.xiaomi.dao.impl.OrderDetailDaoImpl;
import com.cxs.xiaomi.domain.Cart;
import com.cxs.xiaomi.domain.Order;
import com.cxs.xiaomi.domain.OrderDetail;
import com.cxs.xiaomi.service.OrderService;
import cxs.utils.JdbcUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/*
 * @Author:cxs
 * @Date:2021/9/16  15:25
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao = new OrderDaoImpl();
    private OrderDetailDao orderDetailDao = new OrderDetailDaoImpl();
    private CartDao cartDao = new CartDaoImpl();
    private AddressDao addressDao = new AddressDaoImpl();

    @Override
    public boolean add(List<Cart> cartList, Order order) {
        try {
            JdbcUtil.begin();
            // 添加订单
            orderDao.insert(order);
            List ids = new ArrayList();
            // 添加订单详情
            for (Cart cart : cartList) {
                ids.add(cart.getPid());
                OrderDetail orderDetail = new OrderDetail(1,order.getId(),cart.getPid(),cart.getNum(),cart.getMoney());
                orderDetailDao.insert(orderDetail);
            }
            // 删除购物车
            int result = cartDao.deleteByids(ids);
            JdbcUtil.commit();
            return result > 0;
        } catch (SQLException e) {
            try {
                JdbcUtil.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            throw new RuntimeException(e);
        } finally {
            try {
                JdbcUtil.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<Order> getList(Integer id) {
        List<Order> orderList = orderDao.selectAllByUid(id);
        for (Order order : orderList) {
            order.setAddress(addressDao.selectById(order.getAid()));
        }
        return orderList;
    }

    @Override
    public Order getOrder(String oid) {
        return orderDao.selectById(oid);
    }

    @Override
    public boolean updateState(String oid, String state) {
        return orderDao.updateStateById(oid,state) > 0;
    }

    @Override
    public List<Order> getList() {
        List<Order> orderList = orderDao.selectAll();
        for (Order order : orderList) {
            order.setAddress(addressDao.selectById(order.getAid()));
        }
        return orderList;
    }

    @Override
    public List<Order> getList(String condition, List params) {
        List<Order> orderList = orderDao.selectByCondition(condition,params);
        for (Order order : orderList) {
            order.setAddress(addressDao.selectById(order.getAid()));
        }
        return orderList;
    }
}
